package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> selectUserList() throws Exception {
        //1 加载mybatis的核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2 获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3 获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4 执行指定的statement
        List<User> userList = sqlSession.selectList("test.selectUserList");
        System.out.println(userList);
        //5 关闭会话
        sqlSession.close();
        return userList;
    }
    @Override
    public User selectUserById(Integer id) throws Exception {
        //1、加载核心配置文件SqlMapConfig.xml
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2、获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3、获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4、执行指定的statement
        User user = sqlSession.selectOne("test.selectUserById",id);
        System.out.println(user);
        //5、关闭会话
        sqlSession.close();
        return user;
    }
    //模糊查询，#{}和${}的区别：
    //      #{}不需要双引号，可以防止sql注入漏洞
    //      ${}需要包裹在双引号中，不可以防止sql注入漏洞
    @Override
    public List<User> selectUserListLikeName(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("test.selectUserListLikeName",username);
        sqlSession.close();
        return userList;
    }
    @Override
    public List<User> selectUserListLikeName2(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("test.selectUserListLikeName2",username);
        sqlSession.close();
        return userList;
    }
    @Override
    public List<User> selectUserListLikeName3(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("test.selectUserListLikeName3",username);
        sqlSession.close();
        return userList;
    }
    @Override
    public List<User> selectUserListLikeName4(String username) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("test.selectUserListLikeName4",username);
        sqlSession.close();
        return userList;
    }

}
